package dao

import (
	"gowork/studentGrpc/model"
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
	"fmt"
)

var (
	db *gorm.DB
	err error
)

func init(){
	db, err = gorm.Open("mysql", "root:new_password@tcp(localhost:3306)/micro_test")
	if err != nil {
		panic("failed to connect database")
	}
	//defer db.Close()
	db.AutoMigrate(&model.Student{})
}


type StudentDaoImp struct{

}

func (sdi *StudentDaoImp)InesrtDao(s *model.Student)map[string]string{
	db.Create(s)
	return map[string]string{"message":"插入成功", "code":"200"}
}

func (sdi *StudentDaoImp)DeleteDao(name string)map[string]string{
	fmt.Println(name)
	db.Where("name = ?", name).Delete(model.Student{})
	return map[string]string{"message":"删除成功", "code":"200"}
}

func (sdi *StudentDaoImp)UpdateDao(s *model.Student)map[string]string{
	db.Save(s)
	return map[string]string{"message":"修改成功", "code":"200"}
}

func (sdi *StudentDaoImp)QueryDao() []*model.Student{
	var sList []*model.Student
	db.Find(&sList)
	return sList
}


